/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Tomek
 */
public class MyTableModel extends DefaultTableModel {
	Class[] types = new Class [] {
			java.lang.Integer.class, java.lang.Integer.class
	};
	boolean[] canEdit = new boolean [] {
			false, false
	};
	@Override
	public Class getColumnClass(int columnIndex) {
			return types [columnIndex];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
			return canEdit [columnIndex];
	}

	public MyTableModel(){
		super(
				new Integer [][] {
						{1, 4},
						{2, 3},
						{3, 2},
						{4, 1}
				},
				new String [] {
						"Size of ship", "Amount"
				}
		);
	}
	public MyTableModel(TableModel m){
		super(
				new Integer [m.getRowCount()][m.getColumnCount()],
				new String [] {
						"Size of ship", "Amount"
				}
		);
		for(int i = 0; i<m.getRowCount();++i){
			for(int j=0;j<m.getColumnCount();++j){
				setValueAt(m.getValueAt(i, j), i, j);
			}
		}
	}

	void removeOneShip(int size) {
		int i = 0;
		while((Integer)getValueAt(i, 0)<size) ++i;
		if((Integer)getValueAt(i, 0)>size) return;
		if((Integer)getValueAt(i, 1)==1) removeRow(i);
		else this.setValueAt((Integer)getValueAt(i, 1)-1, i, 1);
	}
	void removeShips(){
		while(getRowCount()>0){
			removeRow(0);
		}
	}
}